<template>
  <div class="container">
    <div class="login-wrapper">
      <div class="header">{{ isLogin ? '登录' : '注册' }}</div>
      <div class="form-wrapper">
        <span :class="ishas ? ' ' : 'show'" class="tip">该用户名不可用,已存在!</span>
        <input type="text" @input="inputName" v-model="user.username" name="username" placeholder="用户名"
          class="input-item">
        <input v-model="user.password" type="password" name="password" placeholder="密码" class="input-item">
        <input v-show="!isLogin" v-model="confirmPassword" type="password" name="password" placeholder="确认密码"
          class="input-item">
        <el-button @click="toLogin" class="btn">{{ isLogin ? '登录' : '注册' }}</el-button>
      </div>
      <div class="msg">
        {{ isLogin ? '还没有账号?' : '已有有账号?' }}
        <a v-show="isLogin" @click="isLogin = false">注册</a>
        <a v-show="!isLogin" @click="isLogin = true">登录</a>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { userLogin, userRegister, hasUser } from "@/fetch/user";
import { useRouter } from "@/router/hooks/useRouter";
import { useUser } from "@/store/user";
import { onMounted, reactive, ref, watch } from "vue"
import { useRoute } from "vue-router";
const isLogin = ref(true)
const user = reactive({
  username: 'xia',
  password: '123'
})
const confirmPassword = ref('')
const { goPage } = useRouter()
const { setUser, setToken } = useUser()

const toLogin = async () => {
  if (!isLogin.value) {

    if (user.password != confirmPassword.value) return ElMessage.warning('两次密码不一致')
    const { code, msg } = await userRegister({ ...user })
    if (code == 200) {
      isLogin.value = true
      user.username = user.username
      user.password = user.password
      return ElMessage.success("注册成功,请登录")
    }
    return ElMessage.error(msg)

  }
  const { code, data, msg } = await userLogin({ ...user })
  if (code == 200) {
    goPage("/main")
    setUser(data.data)
    setToken(data.token)
    ElNotification({
      title: '登录成功',
      message: `${data.data.username} 欢迎回来!`,
      type: 'success',
    })
  } else {
    ElNotification({
      title: '登录失败',
      message: msg,
      type: 'error',
    })
  }

}

// 用户名是否重复
const ishas = ref(false)
let inputTimer
// 获取用户名
const inputName = (e: any) => {
  if (isLogin.value) return
  inputTimer && clearTimeout(inputTimer)
  inputTimer = setTimeout(async () => {
    const username = e.target.value
    const { code } = await hasUser(username)
    if (code == 200) return ishas.value = false
    ishas.value = true
  }, 1000);

}


const route = useRoute()
onMounted(() => {
  // false是注册页  true是登录页  0是登录页 1是注册页
  const status = Boolean(Number(route.params?.isLogin))

  isLogin.value = status ? false : true
})


</script>
<style lang="scss" scoped>
.container {
  height: 100%;
  background-image: linear-gradient(to right, #fbc2eb, #a6c1ee);
  font-size: 0.8rem;
}

.form-wrapper {
  position: relative;

  .tip {
    position: absolute;
    color: rgb(173, 68, 68);
    font-size: 12px;
    right: 0;
    top: 10px;
  }

  .show {
    display: none;
  }
}

.login-wrapper {
  background-color: #fff;
  width: 358px;
  height: 588px;
  border-radius: 15px;
  padding: 0 50px;
  position: relative;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.header {
  font-size: 38px;
  font-weight: bold;
  text-align: center;
  line-height: 200px;
}

.input-item {
  display: block;
  width: 100%;
  margin-bottom: 20px;
  border: 0;
  padding: 10px;
  border-bottom: 1px solid rgb(128, 125, 125);
  font-size: 15px;
  outline: none;
}

.input-item:placeholder {
  text-transform: uppercase;
}

.btn {
  text-align: center;
  padding: 10px;
  width: 100%;
  margin-top: 40px;
  background-image: linear-gradient(to right, #a6c1ee, #fbc2eb);
  color: #fff;
}

.msg {
  text-align: center;
  line-height: 88px;
}

a {
  text-decoration-line: none;
  color: #abc1ee;
}
</style>